<!-- eslint-disable vue/multi-word-component-names -->
<!-- eslint-disable eol-last -->
<!-- eslint-disable eol-last -->
<!-- eslint-disable vue/multi-word-component-names -->
<!-- eslint-disable vue/no-parsing-error -->
<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div class="login-container">
    <div class="login-box">
      <h3>智慧学成统计系统</h3>
      <el-form ref="form" :model = "loginFrom" :rules="loginFromRules">
        <el-form-item prop="username">
          <el-input v-model="loginFrom.username" @focus="focusHandlle('username')">
            <img v-act="activrStr === 'username'" slot="prefix" src="@/assets/images/username.png" alt=""/>
          </el-input>
        </el-form-item>
        <el-form-item prop="passwrod">
          <el-input v-model="loginFrom.passwrod" @focus="focusHandlle('pwd')">
            <img v-act="activrStr === 'pwd'" slot="prefix" src="../assets/images/pwd.png" alt="" />
          </el-input>
        </el-form-item>
        <el-form-item prop="verifycode">
          <el-input v-model="loginFrom.verifycode" @focus="focusHandlle('yzm')">
            <img v-act="activrStr === 'yzm'" slot="prefix" src="@/assets/images/yzm.png" alt="" />
            <canvas slot="suffix" id="verifybox"></canvas>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import Gcode from '../utils/verify.js'
import { getItem } from '../utils/storage'
// eslint-disable-next-line semi
export default {
  data () {
    // eslint-disable-next-line no-unused-vars
    const verifyCheck = (rule, value, callback) => {
      // eslint-disable-next-line prefer-const
      let vcode = getItem('verify_code')
      if (value === vcode) {
        // 表示测试通过
        return callback()
      }
      callback(new Error('请输入正确的验证码'))
    }
    return {
      activeStr: '',
      loginFrom: {
        username: '',
        passwrod: '',
        verifycode: ''
      },
      loginFromRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        passwrod: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        verifycode: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { validator: verifyCheck, trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    return new Gcode('#verifybox', 4)
  },
  methods: {
    focusHandlle (val) {
      this.activeStr = val
    },
    login () {
      // 提交数据之前需要做一个表单预校验
      this.$refs.from.validate(async vali => {
        if (!vali) return false
        // eslint-disable-next-line no-unused-vars
        const { data: res } = await this.$http.get('common/login', this.loginFrom)
        if (res.code !== 200) return this.$message.error('登录失败')
        this.$router.push('./home')
      })
    }
  },
  // 自定义选中复选框，图标出现边框
  // eslint-disable-next-line indent
 directives: {
    act: {
      updata: function (el, binding) {
        if (binding.value) {
          el.style.cssText = 'border:1px dashed #ccc'
        } else {
          el.style.cssText = 'border:0'
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  height: 100%;
  background: url("../assets/images/background.png") no-repeat;
  .login-box {
    width: 650px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background-color: rgba(255, 255, 255, 0.89);
    border-radius: 10px;
    .el-form {
      margin: 0 auto;
      width: 385px;
      height: 40px;
      .el-form-item {
        margin-bottom: 22px;
        /deep/ .el-input__inner {
          background-color: transparent;
          border: 0;
          border-bottom: 1px solid #cdcdcd;
          padding-left: 52px;
        }
      }
      .el-button {
        margin-top: 50px;
        width: 100%;
        height: 60px;
        background: linear-gradient(270deg, #5efce8, #736efe);
      }
      img {
        width: 30px;
      }
    }
    > h3 {
      text-align: center;
      margin: 90px 0;
      font-size: 36px;
      color: #1b7bef;
    }
  }
}
// eslint-disable-next-line eol-last, eol-last
</style>
